package com.blog.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.cglib.beans.BeanMap;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Result {


    /**
     * 数据封装
     *
     * @param success 执行结果
     * @param msg     消息
     * @param data    数据
     * @param code    成功码
     * @return
     */
    public static JSONObject responeAjax(boolean success, String msg, Object data, int code) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", success);
        jsonObject.put("code", code);
        jsonObject.put("msg", msg);
        jsonObject.put("data", data);
        return jsonObject;
    }

    /**
     * 数据封装
     *
     * @param success 执行结果
     * @param msg     消息
     * @param data    数据
     * @return
     */
    public static JSONObject responeAjax(boolean success, String msg, Object data) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", success);
        jsonObject.put("msg", msg);
        jsonObject.put("data", data);
        return jsonObject;
    }

    /**
     * 数据封装
     *
     * @param success 执行结果
     * @param msg     消息
     * @param data    数据
     * @param arg    自定义map数据
     * @return
     */
    public static Map<String,Object> responeAjaxMap(boolean success, String msg, Object data,Map<String, Object> arg) {
        Map<String,Object> jsonObject = new HashMap<>();
        jsonObject.put("success", success);
        //为空的时候默认消息
        if (StringUtils.isEmpty(msg)){
            if(success){
                msg = "操作成功";
            }else {
                msg = "操作失败";
            }
        }
        jsonObject.put("msg", msg);
        //默认code 为0
        jsonObject.put("code", 0);
        jsonObject.put("data", data);
        if (arg!=null){
            jsonObject.putAll(arg);
        }
        return jsonObject;
    }

    /**
     * 数据封装
     *
     * @param success 执行结果
     * @param msg     消息
     * @param data    数据
     * @return
     */
    public static JSONObject responeAjax(boolean success, String msg, Object data, Map<String, Object> arg) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", success);
        jsonObject.put("msg", msg);
        jsonObject.put("data", data);
        jsonObject.putAll(arg);
        return jsonObject;
    }


    /**
     * 防跨域数据返回
     *
     * @param response
     * @param obj
     */
    public static void print(HttpServletResponse response, Object obj) {

        response.setHeader("Access-Control-Allow-Credentials", "false");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");

        try {
            PrintWriter out = response.getWriter();
            out.println(JSONObject.fromObject(obj));
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = new HashMap<>();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                if (beanMap.get(key) != null) {
                    map.put(key + "", beanMap.get(key));
                }
            }
        }
        return map;
    }

    /**
     * 将map集合中的数据转化为指定对象的同名属性中
     */
    public static <T> T mapToBean(Map<String, Object> map,Class<T> clazz) throws Exception {
        T bean = clazz.newInstance();
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(map);
        return bean;
    }


    public static <T> Map<String, Object> beanToMap(T bean, String... voKeys) {
        Map<String, Object> map = new HashMap<>();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
                for (Object key : beanMap.keySet()) {
                    for (String voKey : voKeys) {
                        if (beanMap.get(key) != null && !Arrays.toString(voKeys).contains(key.toString())) {
                            map.put(key + "", beanMap.get(key));
                        } else if (voKey == key && beanMap.get(key) != null) {
                        BeanMap beanMap2 = BeanMap.create(beanMap.get(key));
                        for (Object key2 : beanMap2.keySet()) {
                            if (beanMap2.get(key2) != null) {
                                String keyName = key2.toString();
                                if (key2.equals("id")){
                                    keyName = key+"Id";
                                }
                                map.put(keyName + "", beanMap2.get(key2));
                            }
                        }
                    }
                }
            }

        }
        return map;
    }


}
